package com.firsteapps.mobile.android.puzzle;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.firsteapps.mobile.android.puzzle.game.Helper;
import com.firsteapps.mobile.android.puzzle.util.Consts;

public class Scores extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.scores_layout);

		ListView scoresList = (ListView) findViewById(R.id.highscores_list);

		List<HighScore> highScoresList = new ArrayList<HighScore>();
		String[] scoresArray = Helper.getPuzzleData(Scores.this, Consts.FILE_NAME_HIGHSCORES);
		if (scoresArray != null && scoresArray.length > 0) {
			for (String scoreString : scoresArray) {
				highScoresList.add(Helper.getHighScore(scoreString));
			}
		}

		Collections.sort(highScoresList);

		ArrayAdapter<HighScore> adapter = new ArrayAdapter<HighScore>(this, R.layout.list_item_scores, highScoresList) {
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				if (convertView == null) {
					LayoutInflater inflater = (LayoutInflater) Scores.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					convertView = inflater.inflate(R.layout.list_item_scores, null);
				}

				TextView rank = (TextView) convertView.findViewById(R.id.scores_rank);
				TextView moves = (TextView) convertView.findViewById(R.id.scores_movecount);
				TextView time = (TextView) convertView.findViewById(R.id.scores_timecount);
				TextView username = (TextView) convertView.findViewById(R.id.scores_username);
				TextView scoreValue = (TextView) convertView.findViewById(R.id.scores_value);

				HighScore currentItem = getItem(position);
				rank.setText("" + (position + 1));
				username.setText(currentItem.getUsername());
				moves.setText("" + currentItem.getMovesCount());
				time.setText("" + currentItem.getTimePassed());
				scoreValue.setText("" + currentItem.getScore());

				return convertView;
			}
		};

		scoresList.setAdapter(adapter);
	}
}
